package cn.com.ccxcredit.wallet.ui;

import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jFactory;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.http.HttpService;

import java.math.BigDecimal;
import java.math.BigInteger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.ccxcredit.wallet.BaseActivity;
import cn.com.ccxcredit.wallet.R;
import cn.com.ccxcredit.wallet.RequestInstance;
import cn.com.ccxcredit.wallet.observer.SimpleObserve;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BalanceActivity extends BaseActivity {

    @BindView(R.id.et_coin_balance)
    AppCompatEditText etCoinBalance;
    @BindView(R.id.btn_coin_balance)
    AppCompatButton btnCoinBalance;
    @BindView(R.id.tv_coin_balance)
    AppCompatTextView tvCoinBalance;
    @BindView(R.id.btn_trans_count)
    AppCompatButton btnTransCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance);
        ButterKnife.bind(this);
    }


    @OnClick({R.id.btn_coin_balance,R.id.btn_trans_count})
    public void onViewClicked(View view) {
        if(view.getId()==R.id.btn_coin_balance){
            Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> e) throws Exception {
                    Web3j web3j = RequestInstance._instance.admin;
                    EthGetBalance ethGetBalance1 = web3j.ethGetBalance(etCoinBalance.getText().toString().trim(), DefaultBlockParameterName.LATEST).send();
                    BigDecimal balance = new BigDecimal(ethGetBalance1.getBalance().divide(new BigInteger("10000000000000")).toString());
                    final BigDecimal nbalance = balance.divide(new BigDecimal("100000"), 8, BigDecimal.ROUND_DOWN);
                    e.onNext(nbalance.intValue()+"");
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SimpleObserve<String>() {
                        @Override
                        public void onNext(String s) {
                            super.onNext(s);
                            tvCoinBalance.setText(s);
                        }
                    });

        }else if(view.getId()==R.id.btn_trans_count){
            Observable.create(new ObservableOnSubscribe<EthGetTransactionCount>() {
                @Override
                public void subscribe(ObservableEmitter<EthGetTransactionCount> e) throws Exception {
                    final EthGetTransactionCount ethGetTransactionCount =
                            RequestInstance._instance.admin.ethGetTransactionCount(etCoinBalance.getText().toString().trim(),
                                    DefaultBlockParameterName.LATEST).sendAsync().get();
                            e.onNext(ethGetTransactionCount);
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SimpleObserve<EthGetTransactionCount>() {
                        @Override
                        public void onNext(EthGetTransactionCount ethGetTransactionCount) {
                            super.onNext(ethGetTransactionCount);
                            btnTransCount.setText("获取交易数量:"+ethGetTransactionCount.getTransactionCount());
                        }
                    });
           /* new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        Web3j web3j = RequestInstance._instance.admin;

                        final EthGetTransactionCount ethGetTransactionCount =
                                web3j.ethGetTransactionCount(etCoinBalance.getText().toString().trim(),
                                        DefaultBlockParameterName.LATEST).sendAsync().get();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                btnTransCount.setText("获取交易数量:"+ethGetTransactionCount.getTransactionCount());
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }).start();*/
        }

    }


}
